package com.sunday.pinxinghui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sunday.common.utils.DeviceUtils;
import com.sunday.pinxinghui.R;
import com.sunday.pinxinghui.entity.Province;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 刘涛 on 2017/8/29.
 */

public class CityListAdapter extends RecyclerView.Adapter {


    private Context mContext;
    private List<Province> dataSet;
    private View.OnClickListener onClickListener;
    int appWidth;

    public CityListAdapter(Context context, List<Province> data) {
        this.mContext = context;
        this.dataSet = data;
        appWidth= DeviceUtils.getDisplay(mContext).widthPixels;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.list_city_item, null);
        return new CityHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        CityHolder cityHolder= (CityHolder) holder;
        Province city=dataSet.get(position);
        if (position==0){
            cityHolder.index.setVisibility(View.VISIBLE);
            cityHolder.index.setText(city.getIndex());
        }else {
            if (dataSet.get(position-1).getIndex().equals(dataSet.get(position).getIndex())){
                cityHolder.index.setVisibility(View.GONE);
            }else {
                cityHolder.index.setVisibility(View.VISIBLE);
                cityHolder.index.setText(city.getIndex());
            }
        }

        cityHolder.textView.setText(city.getName());
        cityHolder.textView.setOnClickListener(onClickListener);
        cityHolder.textView.setTag(city);
    }

    @Override
    public int getItemCount() {
        return dataSet.size();
    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public class CityHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.index)
        TextView index;
        @Bind(R.id.text_view)
        TextView textView;
        @Bind(R.id.city_layout)
        LinearLayout cityLayout;
        public CityHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
            textView.getLayoutParams().width=appWidth;
        }
    }

}
